﻿var inProgress = false; // Global Array
var CategoryHeaderArray = new Array(); // Global HeaderArray
var categoryPageDetails = ""; // Global Page Details
var _displayColumnLimit = 2;



function DemandMoreRecord(obj, key, categoryId, groupField, pagedetails, async) {

    var keyValue = key.trim();
    var postStr = new formPostStr();
    var hdnCategoryPageSetId = get_object("ctl00_ContentPlaceHolder3_hdnCategoryPageSetId");
    if (keyValue == "CVI") {
        //alert("AM Category Items");
        var hdnPagegroupingField = get_object("ctl00_ContentPlaceHolder3_hdngroupingField");
        postStr.setkeypair("action", "CV");
        postStr.setkeypair("mode", "CVI");
        postStr.setkeypair("pagedetails", pagedetails);
        postStr.setkeypair("CategoryField", hdnPagegroupingField != null ? hdnPagegroupingField.value : "");
        postStr.setkeypair("CategoryStorageValue", groupField);
        postStr.setkeypair("PageSetId", hdnCategoryPageSetId != null ? hdnCategoryPageSetId.value : "");

        AjaxRequestCall(postStr.getPoststr(), categoryId, groupField, "CVI", async);
    } else if (keyValue == "CVH") {
        var hdnPagePermission = get_object("ctl00_ContentPlaceHolder3_hdnPagePermission");
        var hdnPagegroupingField = get_object("ctl00_ContentPlaceHolder3_hdngroupingField");
        var hdnCategoryPaging = get_object("ctl00_ContentPlaceHolder3_hdnCategoryPaging");

        //mainCls.method == "POST" ? mainCls.postData : null
        postStr.setkeypair("action", "CV");
        postStr.setkeypair("mode", "CVH");
        postStr.setkeypair("CategoryField", hdnPagegroupingField != null ? hdnPagegroupingField.value : "");
        //postStr.setkeypair("pagedetails", hdnCategoryPaging != null ? hdnCategoryPaging.value : "");
        postStr.setkeypair("pagedetails", pagedetails);
        postStr.setkeypair("PageSetId", hdnCategoryPageSetId != null ? hdnCategoryPageSetId.value : "");
        AjaxRequestCall(postStr.getPoststr(), "", "", "CVH", async);
    } else {
        return false;
    }
    return true;
}

function AjaxRequestCall(postStr, categoryId, groupField, mode, async) {
    try {

        var strURL = "mb_AjaxHandler.aspx";
        var xmlHttpReq;
        var self = this;
        if (inProgress) {
            return false;
        } else {
            //CVLoadingImage(categoryId, "REQUEST", mode);
        }
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
            self.xmlHttpReq = new XMLHttpRequest();
        }
        // IE
        else if (window.ActiveXObject) {
            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        //self.xmlHttpReq.open('POST', strURL, true);
        var http_date = new Date();
        postStr += "&date=" + http_date;
        //postStr += "&rnd="+Math.random()
        strURL += "?" + postStr;
        strURL += ((strURL.indexOf('?') > 0) ? "&ajts" : "?ajts") + new Date().getTime();

        //self.xmlHttpReq.open('POST', strURL, true);
        //self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

        self.xmlHttpReq.open('GET', strURL, async);
        self.xmlHttpReq.onreadystatechange = function () {

            if (self.xmlHttpReq.readyState == 1) {

                CVLoadingImage(categoryId, "REQUEST", mode, "");
            }

            if (self.xmlHttpReq.readyState == 4) {
                //self.xmlHttpReq = null;
                var response = self.xmlHttpReq.responseText;
                self.xmlHttpReq = null;
                inProgress = false;
                processRequest(response, categoryId, groupField, mode);

                //self.xmlHttpReq = null;
            }
        }
        //self.xmlHttpReq.send(getquerystring());
        //self.xmlHttpReq.send(postStr);
        inProgress = true;
        self.xmlHttpReq.send(null);
    } catch (e) {
        return false;
    }
}


function processRequest(resp, categoryId, groupStorageValue, mode) {

    try {
        
        var respObj = eval("(" + resp.trim() + ")");
        
        var msg = "";
        if (respObj.NORECORD) {
            msg = respObj.NORECORD;
            CVLoadingImage(categoryId, "RESPONSE", mode, msg);
            return false;
        }

        switch (mode) {

        case "CVI":
            //alert("The Response is " + CategoryHeaderArray );
            CategoryHeaderArray.reverse(); // To Pop The First Element Reverse and Pop
            CategoryHeaderArray.pop();
            CategoryHeaderArray.reverse();
            BuildCategoryItems(respObj, mode, categoryId, groupStorageValue);


            if (CategoryHeaderArray.length > 0) {
                if ( CategoryHeaderArray[0].trim() == "" ){
                 var categryId = "CATEGORYNULL_";
                }else{
                var categryId = "CATEGORY" + CategoryHeaderArray[0] + "_";
                }
                
                //var loadMoreRecords = DemandMoreRecord("", "CVI", categryId, CategoryHeaderArray[0], categoryPageDetails ,true)
                var loadMoreRecords = DemandMoreRecord("", "CVI", categryId, CategoryHeaderArray[0], "", true)
            }
            break;

        case "CVH":

            BuildCategoryHeader(respObj, mode, categoryId);
            break;

        default:

            break;

        }


        //CVLoadingImage(categoryId, "RESPONSE", mode, msg);

    } catch (e) {

    }


}






function formPostStr() {
    this.keypair = new Array();
}
formPostStr.prototype.setkeypair = function (key, val) {
    if (this.keypair.length == 0) {
        this.keypair[this.keypair.length] = key + "=" + val;
    } else {
        this.keypair[this.keypair.length] = "&" + key + "=" + val;
    }
}
formPostStr.prototype.getPoststr = function () {
    var keypairstr = "";
    for (var index = 0; index < parseInt(this.keypair.length); index++) {
        keypairstr += this.keypair[index];
    }
    return keypairstr;
}


function CVLoadingImage(cid, key, mode, msg) {
    if (mode == "CVI") {
        var categoryId = "ctl00_ContentPlaceHolder3_" + cid + "MORE";
    } else if (mode == "CVH") {
        var categoryId = "ctl00_ContentPlaceHolder3_" + "CATEGORY_MORE";
    }

    var moredivobj = get_object(categoryId);

    if (key == "REQUEST") {
        //obj.style.display = 'none';
        if (moredivobj) {
            moredivobj.innerHTML = "";
            var imgstr = "<img src=\"Images/load.gif\">Loading...";
            moredivobj.innerHTML = imgstr;

        }
    } else if (key == "RESPONSE") {
        if (moredivobj) {

            moredivobj.innerHTML = "";
            if (msg != "") {
                moredivobj.innerHTML = msg;
            }
/*if (mode == "CVH") {
                moredivobj.innerHTML = "<a href=\"#\">More...</a>";
            } else {
                moredivobj.innerHTML = "<a href=\"#\"><font color='blue'>More...</font></a>";
            }*/
        }
    }

}



function BuildCategoryItems(respObj, mode, categoryId, groupStorageValue) {

    if (respObj.PAGEINFO) {
        pageDetails = respObj.PAGEINFO[0].PAGEDETAILS;
    }
    if (respObj.SCHEMA) {
        var colArray = new Array();
        var colDataType = new Array();
        for (var tindex = 0; tindex < respObj.SCHEMA.length; tindex++) {
            colArray[tindex] = respObj.SCHEMA[tindex].CNAME;
            colDataType[tindex] = respObj.SCHEMA[tindex].DTYPE;
        }

    }

    if (colArray.length > 0) {
        GenerateCategoryItems(colArray, colDataType, categoryId, respObj); // Modified by 'Imran Gani' on 05-June-2012, for the changes of parameter, to pass the Json Object.
    }



    CVLoadingImage(categoryId, "RESPONSE", mode, "");
    //addMoreTag( categoryId , groupStorageValue );
    //alert ("THE CATEGORY ID IS " + categoryId);
    var categoryId = "ctl00_ContentPlaceHolder3_" + categoryId + "MORE";
    var moredivObj = get_object(categoryId);
    if (moredivObj) {
        //    DemandMoreRecord("", "CVI" , categryId , groupStorageValue , "" , true)
        if ( groupStorageValue.trim() == "" ){
         var categryId = "CATEGORYNULL_";
        }
        else{
         var categryId = "CATEGORY" + groupStorageValue + "_";
        }
        moredivObj.innerHTML = "<a href=\"javascript:;\" onclick=\"DemandMoreRecord('','CVI','" + categryId + "','" + groupStorageValue + "','" + pageDetails + "',true" + ")\" ><font color='blue'>More...</font></a>";
    }

}

function GenerateCategoryItems(colArray, colDataType, categoryId, respObj) {

    var categoryTableId = "ctl00_ContentPlaceHolder3_" + categoryId + "TABLE";
    var categoryMoreId = "ctl00_ContentPlaceHolder3_" + categoryId + "MORE";
    var CategryItemTable = get_object(categoryTableId);

    var rowObj = new generateCategoryRow(colArray, colDataType, respObj); // Modified by 'Imran Gani' on 05-June-2012, for the changes of parameter, to pass the Json Object.
    var trRowArray = rowObj.getCategoryRowArray();


    var tbodyObj = CategryItemTable.getElementsByTagName("tbody")[0];

    /*************** To Find The Child Nodes css Start Index *****************/
    //tbodyObj  = tbodyObj.getElementsByTagName("tr");
    //var tBodyChildCount = tbodyObj.childNodes.length;

    var tBodyChildCount = getTrCountValue(tbodyObj.childNodes);

    var cssGridStartIndex = "0";
    if (parseInt(tBodyChildCount) > 0) {
        if (parseInt(tBodyChildCount) % 2 != 0) {
            cssGridStartIndex = "1";
        }
    } /**************************************************************************/

    for (var index = 0; index < trRowArray.length; index++) {
        //CategryItemTable.childNodes[0].appendChild(trRowArray[index]);
        trRowArray[index].className = "Grid_Row" + cssGridStartIndex;

        trRowArray[index].onmouseover = function () {
            this.className = 'Grid_Scroll_BG'
        }

        trRowArray[index].val = cssGridStartIndex;


        trRowArray[index].onmouseout = function () {

            this.className = "Grid_Row" + this.val;

        }
        //trRowArray[index].onmouseout = handlemouseout(this);

        //trRowArray[index].setAttribute("onmouseover", "this.className='Grid_Scroll_BG'");
        //trRowArray[index].setAttribute("onmouseout", "this.className='" + "Grid_Row" + cssGridStartIndex + "'");
        tbodyObj.appendChild(trRowArray[index]);
        cssGridStartIndex = cssGridStartIndex != "0" ? "0" : "1";

    }

}

function getTrCountValue(tBodyObj) {

    var intCount = 0;
    if (tBodyObj.length > 0) {
        for (var index = 0; index < tBodyObj.length; index++) {
            if (tBodyObj[index].tagName == 'TR') {

                intCount++;
            }

        }

    }
    return intCount;
}

function BuildCategoryHeader(respObj, mode, categoryId) {
   
    var hdnPagePermission = get_object("ctl00_ContentPlaceHolder3_hdnPagePermission");
    var hdnPagegroupingField = get_object("ctl00_ContentPlaceHolder3_hdngroupingField");
    var hdnCategoryPaging = get_object("ctl00_ContentPlaceHolder3_hdnCategoryPaging");
    var pageDetails = "";
    var groupFieldColName = hdnPagegroupingField.value;
    if (respObj.PAGEINFO) {

        pageDetails = respObj.PAGEINFO[0].PAGEDETAILS;
        hdnCategoryPaging.value = pageDetails;

    }
    if (respObj.DATA) {
        var dynamicCategoryDiv = get_object("ctl00_ContentPlaceHolder3_CATEGORY_CONTAINER");
        var dynamicMoreDiv = get_object("ctl00_ContentPlaceHolder3_CATEGORY_MORE");
        CategoryHeaderArray = new Array();
        for (var index = 0; index < respObj.DATA.length; index++) {

            var respCategoryName = "";
            var respStorageCategoryName = "";
            if (eval("(" + "respObj.DATA[" + index + "]." + groupFieldColName + "Bind_Storage_Value)")) {

                respStorageCategoryName = eval("(" + "respObj.DATA[" + index + "]." + groupFieldColName + "Bind_Storage_Value)");
                respCategoryName = eval("(" + "respObj.DATA[" + index + "]." + groupFieldColName + ")"); //dynamically get the Object Name
            } else {
                respStorageCategoryName = eval("(" + "respObj.DATA[" + index + "]." + groupFieldColName + ")"); //dynamically get the Object Name
                respCategoryName = respStorageCategoryName;
            }

            CategoryHeaderArray[index] = respStorageCategoryName;
            GenerateCategoryHeader(respCategoryName, respStorageCategoryName, pageDetails, dynamicCategoryDiv);

        }

/* for (var arrindex = 0 ; arrindex <  CategoryIdArray.length ; arrindex++ )
        {
        //debugger;
        var categoryId = "CATEGORY" + CategoryIdArray[arrindex] + "_";
        var loadMoreRecords = DemandMoreRecord("", "CVI", categoryId, CategoryIdArray[arrindex], pageDetails ,true)
        //alert( "AM Builded" + arrindex );
        }*/
        categoryPageDetails = pageDetails;
        var categoryId = "CATEGORY" + CategoryHeaderArray[0] + "_";
        var loadMoreRecords = DemandMoreRecord("", "CVI", categoryId, CategoryHeaderArray[0], "", true) // First Load
        dynamicMoreDiv.innerHTML = "<a href=\"javascript:;\" onclick=\"DemandMoreRecord('','CVH','','','" + pageDetails + "',true)\">More...</a>";

    }

}

/***********Code For Generating the Category Div**********************/

function GenerateCategoryHeader(respCategoryName, respStorageCategoryName, pageDetails, dynamicCategoryDiv) {
    if (dynamicCategoryDiv) {
        var catHolderDiv = new createCategoryDiv(respCategoryName, respStorageCategoryName);
        catHolderDiv.addCategoryCaption();
        catHolderDiv.addCategoryTable();
        catHolderDiv.addCategoryMore();
        dynamicCategoryDiv.appendChild(catHolderDiv.getCreatedCategoryDiv());
    }
}

function createCategoryDiv(CategoryName, respStorageCategoryName) {
    this.holderdiv = document.createElement("div");
    this.holderdiv.style.margin = "0px 0px 10px 0px";
    this.categoryName = CategoryName;
    this.categoryStorageValue = respStorageCategoryName;
}
createCategoryDiv.prototype.addCategoryCaption = function () {
    var CatCaptionDiv = document.createElement("div");
    CatCaptionDiv.className = "Dynamic_PageSectionHeaderRow";
    if ( this.categoryStorageValue.trim() == ""){
    CatCaptionDiv.id = "ctl00_ContentPlaceHolder3_CATEGORYNULL_" + "CAPTION";
    }else{
    CatCaptionDiv.id = "ctl00_ContentPlaceHolder3_CATEGORY" + this.categoryStorageValue + "_" + "CAPTION";
    }
    /*[--No Category--]*/
    if (  this.categoryName.trim() == ""){
    CatCaptionDiv.innerHTML = "[--No Category--]";
    }else{
    CatCaptionDiv.innerHTML = this.categoryName;
    }
    this.holderdiv.appendChild(CatCaptionDiv);
}
createCategoryDiv.prototype.addCategoryTable = function () {
    var CatTable = document.createElement("table");
     if ( this.categoryStorageValue.trim() == ""){
    CatTable.id = "ctl00_ContentPlaceHolder3_CATEGORYNULL_" + "TABLE";
    }else{
    CatTable.id = "ctl00_ContentPlaceHolder3_CATEGORY" + this.categoryStorageValue + "_" + "TABLE";
    }
    CatTable.width = "100%";
    //CatTable.height = "1%";
    CatTable.cellPadding = 0;
    CatTable.cellSpacing = 0;
    CatTable.border = 0;
    var CatTableBody = document.createElement('tbody');
    CatTable.appendChild(CatTableBody);
    this.holderdiv.appendChild(CatTable);
}
createCategoryDiv.prototype.addCategoryMore = function () {
    var CatMoreDiv = document.createElement("div");
    CatMoreDiv.innerHTML = "&nbsp;";
    CatMoreDiv.className = "CategoryMoreDiv";
    if ( this.categoryStorageValue.trim() == ""){
    CatMoreDiv.id = "ctl00_ContentPlaceHolder3_CATEGORYNULL_" +  "MORE";
    }else
    {
    CatMoreDiv.id = "ctl00_ContentPlaceHolder3_CATEGORY" + this.categoryStorageValue + "_" + "MORE";
    }
    this.holderdiv.appendChild(CatMoreDiv);
}
createCategoryDiv.prototype.getCreatedCategoryDiv = function () {
    return this.holderdiv;
}

/******************End of Category Header Function****************/

function generateCategoryRow(colarray, colDatatype, JsonRespObj) {
    this.ColArray = colarray;
    this.ColDataType = colDatatype;
    this.DataRowObj = JsonRespObj.DATA; // Modified by 'Imran Gani' on 05-June-2012.
    if (JsonRespObj.hasOwnProperty("ACTION")) // Below lines are Added by 'Imran Gani' on 05-June-2012, to get action details from the object.
    {
        this.Action = JsonRespObj.ACTION;
    }
    this.trRowArray = new Array();
}

generateCategoryRow.prototype.getCategoryRowArray = function () {
    var hdnCategoryEditPage = get_object("ctl00_ContentPlaceHolder3_hdnCategoryEditPage");
    if (hdnCategoryEditPage) {
        var EditPage = hdnCategoryEditPage.value;
    }
    var url = window.location.search.substring(1);
    var menuId = this.getkeyvalueFromUrl(url, "mn");
    var pgs_id = this.getkeyvalueFromUrl(url, "pgs_id");
    for (var rindex = 0; rindex < this.DataRowObj.length; rindex++) {
        var trObj = document.createElement('tr');
        for (var cindex = 0; cindex < this.ColArray.length; cindex++) {
            if (cindex > _displayColumnLimit) { // Added By Dhanapal TO Limit The Columns Count By 2
                continue;
            }


            var tdObj = document.createElement('td');

            if (cindex > 1) {
                tdObj.width = "30%";
            }

            //alert(this.DataRowObj[rindex].this.ColArray);
            trObj.appendChild(tdObj);
            if (this.ColArray[cindex].trim().toUpperCase() == "KEYS") {

                /* Lines Commanded by 'Imran Gani' on 01-Jun-2012, to avoid eval
                var cellValue = eval('this.DataRowObj[' + rindex + ']' + '.' + this.ColArray[cindex]);
                var firstcellvalue = eval('this.DataRowObj[' + rindex + ']' + '.' + this.ColArray[1]); 
                
                Below Lines are added by 'Imran Gani' on 01-Jun-2012, to get the object value*/
                var cellValue = this.DataRowObj[rindex][this.ColArray[cindex]];
                var firstcellvalue = this.DataRowObj[rindex][this.ColArray[1]];

                trObj.id = "ctl00_ContentPlaceHolder3_CATERGORYAF_ROW_ID _" + cellValue;


                this.appendAccessPermissionIcons(tdObj, cellValue, EditPage, menuId, pgs_id, firstcellvalue);

            } else {

                var displayField = "";
                //var cellValue = eval('this.DataRowObj[' + rindex + ']' + '.' + this.ColArray[cindex]); // Commanded by 'Imran Gani' on 01-Jun-2012, to avoid eval
                var cellValue = this.DataRowObj[rindex][this.ColArray[cindex]]; // Added by 'Imran Gani' on 01-Jun-2012, to get the object value
                //var cellValue = this.DataRowObj[rindex].keys;


                displayField = cellValue;
                if (cindex == 1) // Add Link to the First Column Value For Edit
                {
                    var strText = "";
                    //var keyValue = eval('this.DataRowObj[' + rindex + ']' + '.keys'); // Commanded by 'Imran Gani' on 01-Jun-2012, to avoid eval
                    var keyValue = this.DataRowObj[rindex]['keys']; // Added by 'Imran Gani' on 01-Jun-2012, to get the object value
                    strText = "" + EditPage + "?pgs_id=" + pgs_id + "&PK=" + keyValue;
                    strText += "&mn=" + menuId + "&VMODE=1";
                    displayField = "<a href=\"" + strText + "\"><font color='blue'>" + displayField + "</font></a>";
                }
                if (cellValue == null) {
                    tdObj.innerHTML = "&nbsp";
                } else {
                    //tdObj.appendChild(document.createTextNode(cellValue));
                    tdObj.innerHTML = displayField;
                }
                switch (this.ColDataType[cindex]) {  // Added by 'Imran Gani' on 14-Mar-2014, to set alignment
                    case "BO":
                    case "DA":
                    case "DT":
                    case "TX":
                        tdObj.style.textAlign = 'Left';
                        break;
                    case "CU":
                    case "DC":
                    case "NU":
                    case "AN":
                        tdObj.style.textAlign = 'Right';
                        break;
                    default:
                        tdObj.style.textAlign = 'center';
                        break;
                }
                //tdObj.innerHTML = "<a href=\"#\">HELLO</a>";

            }


        }
        this.setStyle(trObj);
        this.trRowArray[this.trRowArray.length] = trObj;
    }



    return this.trRowArray;
}

generateCategoryRow.prototype.appendAccessPermissionIcons = function (tdObj, cellValues, EditPage, menuId, pgs_id, firstcellvalue) {
    try {
        var hdnCategoryPagePermission = get_object("ctl00_ContentPlaceHolder3_hdnPagePermission");
        var permissionArray = new Array();
        permissionArray = hdnCategoryPagePermission.value.split("|");

        var bRead = "";
        var bCreate = "";
        var bEdit = "";
        var bDelete = "";
        var bAction = ""; // Added by 'Imran Gani' on 04-June-2012, for action button status.

        bRead = permissionArray[0] == "True" ? true : false;
        bCreate = permissionArray[1] == "True" ? true : false;
        bEdit = permissionArray[2] == "True" ? true : false;
        bDelete = permissionArray[3] == "True" ? true : false;
        bAction = permissionArray[4] == "True" ? true : false; // Added by 'Imran Gani' on 04-June-2012, for action button status.

        var tempTableStr = "";
        var EditStr = "";
        var CopyStr = "";
        var DeleteStr = "";
        var appendEditIcons = false;
        tempTableStr = "<table border='0' cellpadding='0' cellspacing='0'><tr>";
        var strText = "";
        if (bEdit) {
            strText = "" + EditPage + "?pgs_id=" + pgs_id + "&PK=" + cellValues;
            strText += "&mn=" + menuId + "";
            tempTableStr += "<td width=\"25px\"><a style='text-decoration:none;' href=\"" + strText + "\">" + "<img border='0' style='cursor:pointer' onclick=\"javascript:this.src='images/load.gif'\" title='Edit' src=\"images/edit.gif\"></a></td>";
            appendEditIcons = true;
        }
        if (bCreate) {
            strText = "" + EditPage + "?pgs_id=" + pgs_id + "&PK=" + cellValues;
            strText += "&mn=" + menuId + "";
            tempTableStr += "<td width=\"25px\"><a style='text-decoration:none;' href=\"" + strText + "\">" + "<img border='0' style='cursor:pointer' onclick=\"javascript:this.src='images/load.gif'\" title='Copy' src=\"images/copy.gif\"></a></td>";
            appendEditIcons = true;
        }
        if (bDelete) {
            strText = "";
            tempTableStr += "<td width=\"25px\"><a style='text-decoration:none;' href=\"javascript:;\" onclick=\"DeleteCategoryRow(this,'" + firstcellvalue + "','" + cellValues + "','" + pgs_id + "','Img_" + cellValues + "_Id')\">" + "<img border='0' id = 'Img_" + cellValues + "_Id' style='cursor:pointer'  title='Delete' src=\"images/delete.gif\"></a></td>";
            appendEditIcons = true;
        }
        if (bAction && this.Action.length > 0) { // Below lines are Added by 'Imran Gani' on 04-June-2012, for action button.
            strText = "";
            tempTableStr += "<td width=\"25px\" onmouseover=\"javascript:ShowDivForActions('div_Ac_" + cellValues + "');\" onmouseout=\"javascript:HideDivForActions('div_Ac_" + cellValues + "');\" ><input id=\"ctl00_ContentPlaceHolder3_Ac_" + cellValues + "\" type=\"image\" name=\"ctl00$ContentPlaceHolder3$Ac_" + cellValues + "\" src=\"images/action_16.gif\" style=\"border-width:0px;\"><br><span id=\"ctl00_ContentPlaceHolder3_div_Ac_" + cellValues + "\" class=\"Grid_Row_actions\" style=\"border-color: graytext; border-width: 1px; border-style: solid; display: none; position: absolute;\" onmouseout=\"javascript:HideDivForActions('div_Ac_" + cellValues + "');\"><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"GrayText\" border=\"0\" style=\"border-bottom:1px;border-top:1px;border-left:1px;border-right:1px;\"><tbody><tr><td nowrap=\"nowrap\"><span class=\"controlFont pgForm_title\" style=\"display:inline-block;width:100%;\"><img src=\"Images/action_16.gif\" style=\"vertical-align:bottom\"><span style=\"vertical-align:bottom\">&nbsp;Actions</span></span></td></tr><tr><td nowrap=\"nowrap\"><span class=\"controlFont Grid_Row_actions\" style=\"display:inline-block;width:100%;\" title=\"" + this.Action[0]["dsc"] + "\">&nbsp; <a href=\"" + this.Action[0]["url"] + "&ID=" + cellValues + "&action=1\">" + this.Action[0]["name"] + "</a> &nbsp;</span><span class=\"Grid_Row_actions\"></span></td></tr></tbody></table></span></td>"
            appendEditIcons = true;
        }
        tempTableStr += "</tr></table>";
        if (appendEditIcons) {
            tdObj.width = '80px';
            tdObj.innerHTML = tempTableStr;
        } else {

            tdObj.parentNode.removeChild(tdObj); // To Remove The DOM Child NOde Table Cell

        }
    } catch (ee) {

    } //tdObj.appendChild(document.createTextNode('Hello Welcomr'));
}

generateCategoryRow.prototype.setStyle = function (trRowArray) {
    if (trRowArray.childNodes.length > 0) {
        trRowArray.childNodes[0].className = 'leftGrid';
    }

    if (trRowArray.childNodes.length > 1) {
        trRowArray.childNodes[parseInt(trRowArray.childNodes.length) - 1].className = 'rightGrid';
        if (trRowArray.childNodes[parseInt(trRowArray.childNodes.length) - 1].innerHTML == "") {
            trRowArray.childNodes[parseInt(trRowArray.childNodes.length) - 1].innerHTML = "&nbsp;"
        }
    }
}

generateCategoryRow.prototype.getkeyvalueFromUrl = function (url, key) {
    try {
        var arrKeySplit = url.split("&");
        for (var index = 0; index < arrKeySplit.length; index++) {
            var QuerykeyValue = arrKeySplit[index].split("=");
            if (QuerykeyValue[0] == key) {
                return arrKeySplit[index].substring(arrKeySplit[index].indexOf('=') + 1);
            }
        }
        //var returnvalue = "";
    } catch (ee) {
        return "";
    }
}


function DeleteCategoryRow(obj, nodeText, key, strPageSetId, imageId) {
    var cmsg = confirm("Are you sure you want to delete " + nodeText + " ?\r Note: Child records will also be deleted.");
    if (cmsg) {
        var url = "DisplayGrid.aspx?pgs_id=" + strPageSetId + "&DPK=" + key;
        var tbodyObject = obj.parentNode.parentNode.parentNode;
        var tCurrRowObject = obj.parentNode.parentNode;
        var DPK = key;
        if (imageId != "") {
            var ImageID = get_object(imageId);
            ImageID.src = "images/Load.gif";
        }
        AJAX_call(obj, url, nodeText);
        //alert( tbodyObject.tagName + "-----" + tCurrRowObject.tagName + "------" + DPK );
        //alert (  " Am Deleting ----  "  +  obj + "---" +  nodeText + "-----" + key ) ;
    } else {

    }

}

function AJAX_call(obj, url, node) {
    if (url == "") {
        return;
    } else {
        url += "&DeleteAjaxTreeCall= true";
    }
    try {
        var xmlHttp_fb;
        var node = node;
        var tree = tree;
        try {
            // Firefox, Opera 8.0+, Safari
            xmlHttp_fb = new XMLHttpRequest();
        } catch (e) {
            // Internet Explorer
            try {
                xmlHttp_fb = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlHttp_fb = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    //alert("Your browser does not support AJAX!");
                    return false;
                }
            }
        }
        xmlHttp_fb.onreadystatechange = function () {
            if (xmlHttp_fb.readyState == 1) {
                //document.getElementById("ctl00_fb_status").innerHTML = "<img src='Images/load.gif' border='0'></img>&nbsp;Submitting...";
            }
            if (xmlHttp_fb.readyState == 4) {
                var responseText = xmlHttp_fb.responseText;
                if (responseText.toUpperCase() == "TRUE") {

                    removeCategoryRowObject(obj);
                }

            }
        }
        var http_date = new Date()
        xmlHttp_fb.open("GET", url, true);
        xmlHttp_fb.send(null);
    } catch (Exxx) {

    }
}


function removeCategoryRowObject(obj) {
    var tCurrRowObject = obj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
    //alert ("The Element Id is " + tCurrRowObject.id );
    var tbodyObject = obj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
    var temptBodyObject = tbodyObject;
    if (tbodyObject) {
        var tCurrRowObject = obj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
        disableDeletedRow(tbodyObject, tCurrRowObject);
    }
}

function disableDeletedRow(tBodyObject, tDeletedRowObject) {

    var childNodelen = tBodyObject.childNodes.length;
    //alert (tBodyObject.tagName + "---" +  childNodelen );
    if (tBodyObject) {

        //var cindexval = 0;
        for (var index = 0; index < tBodyObject.childNodes.length; index++) {

            if (tBodyObject.childNodes[index] === tDeletedRowObject) {

                tBodyObject.childNodes[index].parentNode.removeChild(tBodyObject.childNodes[index]);


                if (getTrPresentStatus(tBodyObject)) {
                    reAssignCss(tBodyObject, index);
                    return;

                } else {
                    removeCategoryHeader(tBodyObject);
                    return;
                }

            }
        }

    }
}

function removeCategoryHeader(tBodyObject) {
    var parentHolderDiv = tBodyObject.parentNode.parentNode;

    parentHolderDiv.parentNode.removeChild(parentHolderDiv);


}

function getTrPresentStatus(tBodyObject) {
    var TrStatus = false;

    for (var index = 0; index < tBodyObject.childNodes.length; index++) {
        if (tBodyObject.childNodes[index].tagName == 'TR') {

            TrStatus = true;

        }

    }
    return TrStatus;
}

function reAssignCss(tBodyObject, rindex) {
    var itemindex = rindex;
    for (index = rindex; index < tBodyObject.childNodes.length; index++) {

        if (tBodyObject.childNodes[index].tagName == 'TR') {


            var className = tBodyObject.childNodes[index].className.trim();

            var previouscssIndex = className.substr(8, 1);

            var cssIndex = previouscssIndex == "1" ? "0" : "1";



            tBodyObject.childNodes[index].className = 'Grid_Row' + cssIndex;

            tBodyObject.childNodes[index].val = cssIndex;



            tBodyObject.childNodes[index].onmouseout = function () {

                this.className = "Grid_Row" + this.val;
                // alert ( this.val );
            }
            itemindex++;
        }
    }
}

/********************Common Methods************************/

function get_object(id) {
    var object = null;
    if (document.layers) {
        object = document.layers[id];
    } else if (document.all) {
        object = document.all[id];
    } else if (document.getElementById) {
        object = document.getElementById(id);
    }
    return object;
}


String.prototype.trim = function () { //For text Trimming 
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function () {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function () {
    return this.replace(/\s+$/, "");
}